package fr.utbm.gi.vi51.project.agent;

import java.util.ArrayList;
import java.util.List;

import fr.utbm.gi.vi51.framework.environment.Environment;
import fr.utbm.gi.vi51.framework.environment.Perception;
import fr.utbm.gi.vi51.project.agent.body.MarineBody;
import fr.utbm.gi.vi51.project.agent.body.MutaliskMissileBody;
import fr.utbm.gi.vi51.project.agent.body.UnitBody;

public class MutaliskMissile extends Projectile {

	private static final long serialVersionUID = 2908218344348411425L;

	public MutaliskMissile(UnitBody targetBody) {
		super(targetBody);
	}

	@Override
	protected MutaliskMissileBody createBody(Environment in) {
		return new MutaliskMissileBody(getAddress());
	}

	@Override
	public void explode() {
		this.getBody().setCurrentIcon(this.getBody().getAttackIcon());
		List<Perception> perceivedObjects = getPerceivedObjects();
		// List of perceived marines
		List<UnitBody> perceivedMarines = new ArrayList<>();
		for (Perception p : perceivedObjects) {
			if (p.getPerceivedObject() instanceof MarineBody) {
				perceivedMarines.add((UnitBody) p.getPerceivedObject());
			}
		}

		for (UnitBody unitBody : perceivedMarines) {
			if (this.getBody().getDamage() > unitBody.getArmor()) {
				unitBody.setHealth(unitBody.getHealth() - this.getBody().getDamage() + unitBody.getArmor());
			}
		}
		// It kills itself
		this.killMe();
	}

}
